package com.zhengyao.controller;

import com.zhengyao.service.GreetingService;
import com.zhengyao.service.GreetingService2;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author : zhengyao3@郑瑶
 * @date : 2020/7/24 10:21
 * @Description: web请求控制类，用于最后去请求dubbo服务端
 */
@RestController
@RequestMapping("/hello")
public class DubboRequestController {
    @Reference
    private GreetingService greetingService;
    @Reference
    private GreetingService2 greetingService2;

    @GetMapping("/greet")
    public String handleRequest() {
        System.out.println(greetingService.sayHello("word"));
        String word2 = greetingService2.sayHello("word2");
        System.out.println(word2);
        return word2;
    }

    @GetMapping("/startTest")
    public String startTest() throws InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(100, 10000, 
                5, TimeUnit.SECONDS, new ArrayBlockingQueue<>(5000));
        while (true) { 
            TimeUnit.MILLISECONDS.sleep(20);
        executor.execute(() -> greetingService.sayHello("hello1"));
        executor.execute(() -> greetingService.sayWorld("world1"));
        executor.execute(() -> greetingService.sayBye("bye1"));
        }
    }
}
